package com.jldx.oa.controller;

import java.util.Date;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jldx.common.Result;
import com.jldx.oa.bean.Article;

public class ArticleController extends BaseController{

	public void index(){
		render("/WEB-INF/views/article.html");
	}
	
	public void query(){
		Integer pageNumber = getPage();
		String title = getPara("title");
		Integer limit = getLimit();
		Kv cond = Kv.by("title", title);
		SqlPara sqlPara = Db.getSqlPara("oa.articleList",cond);
		Page<Article> page = Article.dao.paginate(pageNumber, limit, sqlPara);
		Result result = Result.cast(page);
		renderJson(result);
	}
	
	public void edit(){
		Integer id = getParaToInt("id");
		if(id!=null){
			Article article = Article.dao.findById(id);
			setAttr("article", article);
		}
		view("article_edit.html");
	}
	
	public void save(){
		Article article = getModel(Article.class,"");
		if(article.getId()!=null){
			article.update();
		}else{
			article.setCreateTime(new Date());
			article.save();
		}
		renderText("suc");
	}
	
	public void delete(){
		Integer id = getParaToInt("id");
		boolean result = false;
		if(id!=null){
			result = Article.dao.deleteById(id);
		}
		if(result){
			renderText("suc");
		}else{
			render("fail");
		}
	}
}
